package org.jeecg.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.model.EnumDictOption;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysClient;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.service.ISysClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 客户端表 前端控制器
 * </p>
 *
 * @Author sean
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/sys/client")
@Slf4j
public class SysClientController extends JeecgController<SysClient, ISysClientService> {

	@Autowired
	private ISysClientService sysClientService;

	/**
	  * 分页列表查询
	 * @param client
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysClient>> queryPageList(SysClient client,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		QueryWrapper<SysClient> queryWrapper = QueryGenerator.initQueryWrapper(client, req.getParameterMap());
		Page<SysClient> page = new Page<SysClient>(pageNo, pageSize);
		IPage<SysClient> pageList = sysClientService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 获取客户端字段
	 * @return
	 */
	@RequestMapping(value = "/options", method = RequestMethod.GET)
	public Result<List<EnumDictOption>> option() {
		List<EnumDictOption> optionList = sysClientService.options();
		return Result.OK(optionList);
	}
	
	/**
	  *   添加
	 * @param client
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	//@RequiresRoles({"admin"})
	public Result<SysClient> add(@RequestBody SysClient client) {
		sysClientService.save(client);
		return Result.OK("添加成功！");
	}
	
	/**
	  *  编辑
	 * @param client
	 * @return
	 */
	//@RequiresRoles({"admin"})
	@RequestMapping(value = "/edit",method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<SysClient> edit(@RequestBody SysClient client) {
		sysClientService.updateById(client);
		return Result.OK("编辑成功!");
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	//@RequiresRoles({"admin"})
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysClientService.removeById(id);
		return Result.ok("删除角色成功");
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	//@RequiresRoles({"admin"})
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public Result<SysClient> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		sysClientService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryById", method = RequestMethod.GET)
	public Result<SysClient> queryById(@RequestParam(name="id",required=true) String id) {
		SysClient sysClient = sysClientService.getById(id);
		if(sysClient==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(sysClient);
	}

	/**
	 * 校验角色编码唯一
	 */
	@RequestMapping(value = "/checkClientCode", method = RequestMethod.GET)
	public Result<Boolean> checkClientCode(String id,String clientCode) {
		Result<Boolean> result = new Result<>();
		//如果此参数为false则程序发生异常
		result.setResult(true);
		log.info("--验证角色编码是否唯一---id:"+id+"--clientCode:"+clientCode);
		try {
			SysClient client = null;
			if(oConvertUtils.isNotEmpty(id)) {
				client = sysClientService.getById(id);
			}
			SysClient newClient = sysClientService.getOne(new QueryWrapper<SysClient>().lambda().eq(SysClient::getClientCode, clientCode));
			if(newClient != null) {
				//如果根据传入的roleCode查询到信息了，那么就需要做校验了。
				if(client==null) {
					//role为空=>新增模式=>只要roleCode存在则返回false
					result.setSuccess(false);
					result.setMessage("客户端编码已存在");
					return result;
				}else if(!id.equals(newClient.getId())) {
					//否则=>编辑模式=>判断两者ID是否一致-
					result.setSuccess(false);
					result.setMessage("客户端编码已存在");
					return result;
				}
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(false);
			result.setMessage(e.getMessage());
			return result;
		}
		result.setSuccess(true);
		return result;
	}


	/**
	 * 导出excel
	 * @param request
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(SysClient sysRole,HttpServletRequest request) {
		return super.exportXls(request, sysRole, SysClient.class, "电子围栏");
	}

	/**
	 * 通过excel导入数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, SysClient.class);
	}

	
}
